package main

import (
    "../detector"
    "gopkg.in/alecthomas/kingpin.v2"
    "../common"
    "fmt"
)

var (
    zkAddr   = kingpin.Flag("zk", "database name").Strings()
    node     = kingpin.Flag("node", "node").String()
    zkPath   = kingpin.Flag("metapath", "meta path").String()
    cliAddrs = kingpin.Flag("cliaddrs", "cli addrs").Strings()
)

//./detector_meta_test --zk="127.0.0.1:2181" --node="127.0.0.1:3212" --metapath="/meta" --cliaddrs="127.0.0.1:3001" --cliaddrs="127.0.0.1:3002"

func main() {
    kingpin.Parse()
    zkOpt := detector.ZkOption{
        ZkAddresses:                 *zkAddr,
        ZkMetaPath:                  *zkPath,
        ZkTimeoutMilliseconds:       3000,
        ZkCheckIntervalMilliseconds: 1000,
    }
    opt := detector.Option{
        BeaconIntervalMilliseconds: 100,
        CheckIntervalMilliseconds:  100,
        LeaseMilliseconds:          200,
        DelayMilliseconds:          120,
        GraceMilliseconds:          150,
    }
    fmt.Println("===========")
    mfd := detector.NewFailureDetector(*node, opt,
        func(n common.Node) {
            fmt.Printf(" %v Connected\n", n)
        },
        func(n []common.Node) {
            fmt.Printf(" %v Disconnected\n", n)
        })
    for _, n := range *cliAddrs {
        mfd.Register(common.Node(n))
    }
    go mfd.Start()
    defer mfd.Stop()
    fd := detector.NewZkDetectorOnMeta(common.Node(*node),zkOpt,func(a bool) { fmt.Printf("Set Active: %v\n", a) })
    fd.Start()
    defer fd.Stop()
    
}
